package com.sharad.quizbowl.ui.client.widget;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.sharad.quizbowl.ui.client.util.Resources;
import com.sharad.quizbowl.ui.client.widget.event.FilterEvent;
import com.sharad.quizbowl.ui.client.widget.event.FilterEventHandler;

public class SimpleSearch extends Composite {
	private HandlerManager handlerManager;
	public static final String HORIZONTAL = "horizontal";
	public static final String VERTICAL = "vertical";
	private String configuration = VERTICAL;

	private static SimpleSearchUiBinder uiVertical = GWT
			.create(SimpleSearchUiBinder.class);

	@UiTemplate("SimpleSearch.ui.xml")
	interface SimpleSearchUiBinder extends UiBinder<Widget, SimpleSearch> {
	}

	private static SimpleSearchHorizontalUiBinder uiHorizontal = GWT
			.create(SimpleSearchHorizontalUiBinder.class);

	@UiTemplate("SimpleSearchHorizontal.ui.xml")
	interface SimpleSearchHorizontalUiBinder extends
			UiBinder<Widget, SimpleSearch> {
	}

	public SimpleSearch() {
		this("Search");
	}

	@UiField(provided = true)
	Image loading = new Image(Resources.IMAGE_LOADING);
	@UiField
	Button button;
	@UiField
	TextBox searchBox;
	HorizontalPanel main;

	public SimpleSearch(String buttonText) {
		main = new HorizontalPanel();
		main.setWidth("100%");
		main.setHeight("100%");
		handlerManager = new HandlerManager(this);
		main.add(uiVertical.createAndBindUi(this));
		initWidget(uiVertical.createAndBindUi(this));
		button.setText(buttonText);
		loading.setUrl(Resources.IMAGE_LOADING);
		loading.setVisible(false);
		setWidget(uiHorizontal.createAndBindUi(this));

	}

	@UiHandler("button")
	void onClick(ClickEvent e) {
		doSearch();
	}

	@UiHandler("searchBox")
	void onKeyPress(KeyPressEvent e) {
		if (e.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER)
			doSearch();
	}

	public void doSearch() {
		HashMap<String, List<String>> parameters = new HashMap<String, List<String>>();
		parameters.put("answer",
				Arrays.asList(new String[] { searchBox.getValue() }));
		FilterEvent e = new FilterEvent(parameters);
		fireEvent(e);
	}

	@Override
	public void fireEvent(GwtEvent<?> e) {
		handlerManager.fireEvent(e);
	}

	public HandlerRegistration addFilterEventHandler(FilterEventHandler handler) {
		return handlerManager.addHandler(FilterEvent.TYPE, handler);
	}

	public Image getLoading() {
		return loading;
	}

	public void setConfiguration(String config) {
		if (config.equals(HORIZONTAL)) {
			if (!configuration.equals(config)) {
				main.clear();
				main.add(uiHorizontal.createAndBindUi(this));
			}
		} else if (config.equals(VERTICAL)) {
			if (!configuration.equals(config)) {
				main.clear();
				main.add(uiVertical.createAndBindUi(this));
			}
		}
	}

}
